<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){ 
    	$articleList = M('article')->field(array('id','title'))->select();
    	$this->assign('artlist',$articleList);
		$this->display();
    }
    public function details($id){
    	$field = M('article')->find($id);
    	$field['article'] = htmlspecialchars_decode($field['article']);
    	$this->createHtml("http://www.baidu.com","html","baidu");
    	$this->assign('field',$field);
    	$this->display();
    }
    private function createHtml($url,$dir,$filename){ 
	    $content = file_get_contents($url);                //获取内容        
	    if(!is_dir($dir)){                                 //如果目录不存在  
	        mkdir(iconv("UTF-8", "GBK", $dir),0777,true);   //创建目录(777权限)  
	    }
	    //生成的文件路径与文件名  
	    $path = $dir."/".$filename.".html";  
	    if(file_exists($path)){  
	        unlink($path);                   //删除已有的同名文件  
	    }  
	    //'w' 写入方式打开，将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。    
	    $fp = fopen($path, "w");               
	    $flag = fwrite($fp, $content);       //写入内容  
	    fclose($fp);  
	}
}